package self.hoy.demo.grainmall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.product.mapper.SpuImagesMapper;
import self.hoy.demo.grainmall.product.entity.SpuImages;
import self.hoy.demo.grainmall.product.service.SpuImagesService;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImages> implements SpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImages> page = this.page(
                new Query<SpuImages>().getPage(params),
                new QueryWrapper<SpuImages>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveImages(Long spuId, List<String> images) {
        if (images == null || images.isEmpty()) {

        } else {
            Integer sort = 0;
            List<SpuImages> list = images.stream().filter(i->{
                return !StringUtils.isEmpty(i);
            }).map(i->{
                SpuImages image = new SpuImages();
                image.setSpuId(spuId);
//                image.setImgName();
                image.setImgUrl(i);
//                image.setImgSort(sort++);
//                image.setDefaultImg();
                return image;
            }).collect(Collectors.toList());
            this.saveBatch(list);
        }

    }

}